/**
 * File name: Node.java Author: Willard Wang Create date: May 22, 2013
 */
package edu.sjtu.se.prophet.graph;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author deathspeeder
 * 
 */
public class Node implements Serializable, Comparable<Node> {
    /**
     * 
     */
    private static final long serialVersionUID = 6248714812583856194L;
    private String id;
    protected Set<Node> inNodes = new TreeSet<Node>();
    protected Set<Node> outNodes = new TreeSet<Node>();
    protected Map<String, Object> metrics = new HashMap<String, Object>();

    public Node(String id) {
        this.id = id;
    }

    @Override
    public int compareTo(Node o) {
        return this.hashCode() - o.hashCode();
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj instanceof Node) {
            Node objNode = (Node) obj;
            return id.equals(objNode.id);
        }
        return false;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Set<Node> getInNodes() {
        return inNodes;
    }

    public void setInNodes(Set<Node> inNodes) {
        this.inNodes = inNodes;
    }

    public Set<Node> getOutNodes() {
        return outNodes;
    }

    public void setOutNodes(Set<Node> outNodes) {
        this.outNodes = outNodes;
    }

    public Map<String, Object> getMetrics() {
        return metrics;
    }

    public void setMetrics(Map<String, Object> metrics) {
        this.metrics = metrics;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(id + ": in[");
        for (Node n : inNodes) {
            builder.append(n.id + ",");
        }
        if (builder.charAt(builder.length() - 1) == ',')
            builder.deleteCharAt(builder.length() - 1);
        builder.append("] out[");
        for (Node n : outNodes) {
            builder.append(n.id + ",");
        }
        if (builder.charAt(builder.length() - 1) == ',')
            builder.deleteCharAt(builder.length() - 1);
        builder.append("] metrics[");
        for (String key : metrics.keySet()) {
            builder.append(key + ":" + metrics.get(key) + " ");
        }
        builder.append("]");
        return builder.toString();
    }

}
